package scau.erps.view.decision;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import scau.erps.app.Erps;
import scau.erps.view.decision.card.DeliverCardPanel;
import scau.erps.view.decision.card.FactoryCardPanel;
import scau.erps.view.decision.card.LineCardPanel;
import scau.erps.view.decision.card.LongLoanCardPanel;
import scau.erps.view.decision.card.ManufactureCardPanel;
import scau.erps.view.decision.card.MaterialCardPanel;
import scau.erps.view.decision.card.OrderCardPanel;
import scau.erps.view.decision.card.ResearchCardPanel;
import scau.erps.view.decision.card.ShortLoanCardPanel;

/**
 * 练习模式对话框
 * @author DCJ
 *
 */
public class DecisionFrame extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JButton nextStepBtn;
	private JPanel contentPanel,cardPanel,controlPanel,stepPanel,schedulePanel;
	private JLabel scheduleLabel;
	private CardLayout card = new CardLayout();
	public DecisionFrame() {
		this.setTitle("ERP沙盘决策");
		this.setSize(800, 650);	
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		contentPanel = new JPanel(new BorderLayout());		
		this.setContentPane(contentPanel);
		contentPanel.add(getCardPanel(), BorderLayout.CENTER);
		contentPanel.add(getControlPanel(), BorderLayout.SOUTH);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.showCardPanel();
		this.setVisible(true);
	}
	private JPanel getCardPanel(){
		cardPanel = new JPanel();
		cardPanel.setLayout(card);
		OrderCardPanel orderCard = new OrderCardPanel();
		ShortLoanCardPanel shortLoanCard = new ShortLoanCardPanel();	
		MaterialCardPanel materialCard = new MaterialCardPanel();
		LineCardPanel lineCard = new LineCardPanel();
		ManufactureCardPanel manufactuedCard = new ManufactureCardPanel();
		DeliverCardPanel deliverCard = new DeliverCardPanel();
		LongLoanCardPanel longLoanCard = new LongLoanCardPanel();
		FactoryCardPanel factoryCard = new FactoryCardPanel();
		ResearchCardPanel researchCard = new ResearchCardPanel();
		cardPanel.add(orderCard,"order");		
		cardPanel.add(shortLoanCard,"shortLoan");
		cardPanel.add(materialCard,"material");
		cardPanel.add(lineCard,"line");
		cardPanel.add(manufactuedCard,"manufacture");
		cardPanel.add(deliverCard,"deliver");
		cardPanel.add(longLoanCard,"longLoan");
		cardPanel.add(factoryCard,"factory");
		cardPanel.add(researchCard,"research");		
		return cardPanel;
	} 
	private JPanel getControlPanel(){
		controlPanel = new JPanel();
		controlPanel.setLayout(new GridLayout(1,3));
		stepPanel = new JPanel();
		stepPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		nextStepBtn = new JButton("下一步");	
		nextStepBtn.addActionListener(this);	
		stepPanel.add(nextStepBtn);
		schedulePanel = new JPanel();
		schedulePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		JLabel label = new JLabel("当前进度：");
		scheduleLabel = new JLabel();
		this.setSchedule();
		schedulePanel.add(label);
		schedulePanel.add(scheduleLabel);
		controlPanel.add(schedulePanel);
		controlPanel.add(stepPanel);
		return controlPanel;
	}
	private void showCardPanel(){
		int step = Integer.parseInt(Erps.getSand().getStep());
		if(step>=1 && step<=4){
			card.show(cardPanel, "order");
		}
		else if(step>=5 && step<=7){
			card.show(cardPanel, "shortLoan");
		}
		else if(step>=8 && step<=12){
			card.show(cardPanel, "material");
		}
		else if(step>=13 && step<=17){
			card.show(cardPanel, "line");
		}	
		else if(step>=18 && step<=19){
			card.show(cardPanel, "manufacture");
		}
		else if(step>=20 && step<=23){
			card.show(cardPanel, "deliver");
		}
		else if(step>=24 && step<=26){
			card.show(cardPanel, "longLoan");
		}	
		else if(step>=27 && step<=30){
			card.show(cardPanel, "factory");
		}
		else if(step>=31 && step<=33){
			card.show(cardPanel, "research");
		}	
		
	}
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == nextStepBtn) {
			card.next(cardPanel);
		}			
	}
	private void setSchedule(){
		String time = Erps.getSand().getTime();
		String step = Erps.getSand().getStep();
		scheduleLabel.setText(time+"-"+step);
	}
}